package com.example.demo;

import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/User")
public class Userlogin {
    @RequestMapping("/login")
    public Boolean login(String username, String password, HttpSession httpSession){
        if(StringUtil.isNullOrEmpty(username)||StringUtil.isNullOrEmpty(password)){
            return false;
        }
        if("admin".equals(username)&&"123456".equals(password)){
            //注入内容
            httpSession.setAttribute("Userlogins",username);
            return true;
        }
        return false;
    }
    @RequestMapping("/getAttribute")
    public String getAttribute(HttpSession httpSession, HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession(false);
        if(session==null)return "";
        return "用户名称为:"+(String) httpSession.getAttribute("Userlogins");
    }
}
